
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for look_and_feel.
	''' </summary>
	Public Partial Class look_and_feel
		Inherits BaseWebPage


		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here
			' this.pageTemplate.PageTitle = PageTitles.LOOK_AND_FEEL_PAGE;

			Dim usr As User = Me.LoggedUser

			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			Me.initPageTitles(Constants.LOOK_AND_FEEL, "", Form1, Nothing, Nothing)
			Me.initAdvertisements(Form1, Nothing)

			If Not Me.IsPostBack Then
				BindList()
			End If
		End Sub


		Public Function CreateDataSource() As ICollection

			Dim usr As BO.User = LoggedUser
			Dim arrTemplates As ArrayList = Common.Utils.GetTemplates()
			Dim startIndex As Integer = 0
			Dim endIndex As Integer = arrTemplates.Count
			Dim i As Integer
			Dim dt As New DataTable()
			Dim dr As DataRow

			dt.Columns.Add(New DataColumn("ThemeImageURL", GetType(String)))
			dt.Columns.Add(New DataColumn("ThemeName", GetType(String)))
			dt.Columns.Add(New DataColumn("ThemeID", GetType(String)))
			dt.Columns.Add(New DataColumn("isDefault", GetType(Boolean)))

			Dim imagesOnLastRow As Integer = 0
			Dim t As Integer = 0

			t = (endIndex - startIndex)
			Math.DivRem(t, Constants.MAX_TEMPLATES_ON_ROW, imagesOnLastRow)

			i = 0

			For Each tpl As Template In arrTemplates
				dr = dt.NewRow()

				dr(0) = "../" + Constants.IMAGE_FOLDER + "/" + tpl.TemplatePicture
				dr(1) = HttpUtility.HtmlEncode(tpl.Name)
				dr(2) = tpl.ID
				If usr.TemplateID = tpl.ID Then
					dr(3) = True
				Else
					dr(3) = False
				End If

				dt.Rows.Add(dr)
				System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
			Next

			Dim dv As New DataView(dt)
			Return dv
		End Function

		Public Sub BindList()
			dlThemes.DataSource = CreateDataSource()
			dlThemes.DataBind()

			Dim arrTemplates As ArrayList = Common.Utils.GetTemplates()
			Dim btnDefaultTemp As Button
			Dim lblTemp As Label
			Dim btnTemp As HyperLink
			Dim i As Integer = 0
			While i < arrTemplates.Count
				btnTemp = DirectCast(dlThemes.Items(i).FindControl("hlThemeImage"), HyperLink)
				btnDefaultTemp = DirectCast(dlThemes.Items(i).FindControl("btnSetDefault"), Button)
				lblTemp = DirectCast(dlThemes.Items(i).FindControl("lblDefaultTheme"), Label)
				If btnTemp.Attributes("isDefault").Equals("True") Then
					btnDefaultTemp.Visible = False
					lblTemp.Visible = True
				Else
					btnDefaultTemp.Visible = True
					lblTemp.Visible = False
				End If
				System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
			End While
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.dlThemes.ItemCommand, AddressOf Me.dlThemes_ItemCommand

		End Sub
#End Region

		Public Sub dlThemes_ItemCommand(ByVal Sender As Object, ByVal e As DataListCommandEventArgs)
			Dim cmd As String = e.CommandName
			Dim usr As BO.User = LoggedUser
			If cmd = "SetDefault" Then
				usr.TemplateID = Convert.ToInt32(e.CommandArgument)
				usr.EditDate = DateTime.Now
				usr.Save()
				lblMessage.Visible = True
			End If
			BindList()
		End Sub

		====================
		' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable Form1.
		====================
		Public ReadOnly Property Form1() As System.Web.UI.HtmlControls.HtmlForm
			Get
				Return Migrated_Form1
			End Get
		End Property
		'set { Migrated_Form1 = value; }
	End Class
End Namespace






